/*
 * XJSTLHelper.h
 *
 *  Created on: 2015年9月24日
 *      Author: Administrator
 */

#ifndef SRC_XJSTLHELPER_H_
#define SRC_XJSTLHELPER_H_

#include "XJEnv.h"
#include "XJSingleton.h"
#include <math.h>
#include <memory>
#include <string>
#include <functional>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#ifdef _GLIBCXX_PERMIT_BACKWARD_HASH
#include <hash_set>
#include <hash_map>
#endif

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <unordered_set>
#include <unordered_map>
#endif

// using namespace std;
// #ifdef __GNUC__
// using namespace __gnu_cxx;
// #else
// using namespace stdext;
// using namespace std::tr1;
// #endif

namespace XJUtilClass {

typedef	std::wstring		CStdStringW;
typedef std::string		CStdStringA;
#ifdef _UNICODE
	typedef	CStdStringW		CStdString;
#else
	typedef CStdStringA		CStdString;
#endif


typedef std::list<short>						short_list;
typedef std::list<int>						int_list;
typedef std::list<long>						long_list;
typedef std::list<XJ_INT64_T>					int64_list;
typedef std::list<unsigned short>			ushort_list;
typedef std::list<unsigned int>				uint_list;
typedef std::list<unsigned long>				ulong_list;
typedef std::list<XJ_UINT64_T>			uint64_list;
typedef std::list<float>						float_list;
typedef std::list<double>					double_list;
typedef std::stack<short>					short_stack;
typedef std::stack<int>						int_stack;
typedef std::stack<long>						long_stack;
typedef std::stack<XJ_INT64_T>					int64_stack;
typedef std::stack<unsigned short>			ushort_stack;
typedef std::stack<unsigned int>				uint_stack;
typedef std::stack<unsigned long>			ulong_stack;
typedef std::stack<XJ_UINT64_T>			uint64_stack;
typedef std::stack<float>					float_stack;
typedef std::stack<double>					double_stack;
typedef std::queue<short>					short_queue;
typedef std::queue<int>						int_queue;
typedef std::queue<long>						long_queue;
typedef std::queue<XJ_INT64_T>					int64_queue;
typedef std::queue<unsigned short>			ushort_queue;
typedef std::queue<unsigned int>				uint_queue;
typedef std::queue<unsigned long>			ulong_queue;
typedef std::queue<XJ_UINT64_T>			uint64_queue;
typedef std::queue<float>					float_queue;
typedef std::queue<double>					double_queue;
typedef std::deque<short>					short_deque;
typedef std::deque<int>						int_deque;
typedef std::deque<long>						long_deque;
typedef std::deque<XJ_INT64_T>					int64_deque;
typedef std::deque<unsigned short>			ushort_deque;
typedef std::deque<unsigned int>				uint_deque;
typedef std::deque<unsigned long>			ulong_deque;
typedef std::deque<XJ_UINT64_T>			uint64_deque;
typedef std::deque<float>					float_deque;
typedef std::deque<double>					double_deque;
typedef std::vector<short>					short_vector;
typedef std::vector<int>						int_vector;
typedef std::vector<long>					long_vector;
typedef std::vector<XJ_INT64_T>					int64_vector;
typedef std::vector<unsigned short>			ushort_vector;
typedef std::vector<unsigned int>			uint_vector;
typedef std::vector<unsigned long>			ulong_vector;
typedef std::vector<XJ_UINT64_T>		uint64_vector;
typedef std::vector<float>					float_vector;
typedef std::vector<double>					double_vector;
typedef std::set<short>						short_set;
typedef std::set<int>						int_set;
typedef std::set<long>						long_set;
typedef std::set<XJ_INT64_T>					int64_set;
typedef std::set<unsigned short>				ushort_set;
typedef std::set<unsigned int>				uint_set;
typedef std::set<unsigned long>				ulong_set;
typedef std::set<XJ_UINT64_T>			uint64_set;
typedef std::set<float>						float_set;
typedef std::set<double>						double_set;
#ifdef _GLIBCXX_PERMIT_BACKWARD_HASH
typedef hash_set<short>					short_hash_set;
typedef hash_set<int>					int_hash_set;
typedef hash_set<long>					long_hash_set;
typedef hash_set<XJ_INT64_T>				int64_hash_set;
typedef hash_set<unsigned short>		ushort_hash_set;
typedef hash_set<unsigned int>			uint_hash_set;
typedef hash_set<unsigned long>			ulong_hash_set;
typedef hash_set<XJ_UINT64_T>		uint64_hash_set;
typedef hash_set<float>					float_hash_set;
typedef hash_set<double>				double_hash_set;
#endif
#ifdef __GXX_EXPERIMENTAL_CXX0X__
typedef std::unordered_set<short>			short_unordered_set;
typedef std::unordered_set<int>				int_unordered_set;
typedef std::unordered_set<long>				long_unordered_set;
typedef std::unordered_set<XJ_INT64_T>			int64_unordered_set;
typedef std::unordered_set<unsigned short>	ushort_unordered_set;
typedef std::unordered_set<unsigned int>		uint_unordered_set;
typedef std::unordered_set<unsigned long>	ulong_unordered_set;
typedef std::unordered_set<XJ_UINT64_T>	uint64_unordered_set;
typedef std::unordered_set<float>			float_unordered_set;
typedef std::unordered_set<double>			double_unordered_set;
#endif

/*****************************************************************************/
/******************************** 容器操作函数 *******************************/

/**********************************
描述: 清除普通集合 , 适用于 vector<Object> / list<Object>
参数:
	v		: vector / list / set

返回值:
**********************************/
template<class Set> void ClearSet(Set& v)
{
	v.clear();
}

template<class Set> struct Set_Cleaner
{
	static void Clear(Set& v) {ClearSet(v);}
};

/**********************************
描述: 清除指针集合 (清除前先释放指针), 适用于 vector<Object*> / list<Object*>
参数:
		v		: vector / list / set

返回值:
**********************************/
template<class PtrSet> void ClearPtrSet(PtrSet& v)
{
	typename PtrSet::iterator	it;
	for(it = v.begin();
							it != v.end();
							++it)
		delete (*it);

	v.clear();
}

template<class PtrSet> struct PtrSet_Cleaner
{
	static void Clear(PtrSet& v) {ClearPtrSet(v);}
};

/**********************************
描述: 清除指针集合 (指针同时又指向数组), 适用于 vector<Object*[]> / list<Object*[]>
参数:
		v		: vector / list / set

返回值:
**********************************/
template<class PtrArraySet> void ClearPtrArraySet(PtrArraySet& v)
{
	for(typename PtrArraySet::iterator	it = v.begin();
								it != v.end();
								++it)
		delete[] (*it);

	v.clear();
}

template<class PtrArraySet> struct PtrArraySet_Cleaner
{
	static void Clear(PtrArraySet& v) {ClearPtrArraySet(v);}
};

/**********************************
描述: 清除普通影射 , 适用于 map<key, value>
参数:
	v		: map

返回值:
**********************************/
template<class Map> void ClearMap(Map& v)
{
	v.clear();
}

template<class Map> struct Map_Cleaner
{
	static void Clear(Map& v) {ClearMap(v);}
};

/**********************************
描述: 清除指针影射 (清除前先释放指针), 适用于 map<key, Object*>
参数:
		v		: map

返回值:
**********************************/
template<class PtrMap> void ClearPtrMap(PtrMap& v)
{
	for(typename PtrMap::iterator	it = v.begin();
							it != v.end();
							++it)
		delete it->second;

	v.clear();
}

template<class PtrMap> struct PtrMap_Cleaner
{
	static void Clear(PtrMap& v) {ClearPtrMap(v);}
};

/**********************************
描述: 清除指针影射 (指针同时又指向数组), 适用于 map<key, Object*[]>
参数:
		v		: map

返回值:
**********************************/
template<class PtrArrayMap> void ClearPtrArrayMap(PtrArrayMap& v)
{
	for(typename PtrArrayMap::iterator	it = v.begin();
								it != v.end();
								++it)
		delete[] it->second;

	v.clear();
}

template<class PtrArrayMap> struct PtrArrayMap_Cleaner
{
	static void Clear(PtrArrayMap& v) {ClearPtrArrayMap(v);}
};

/**********************************
描述: 清除集合-集合 (清除前先清除内部集合), 适用于 set<vector<Object>*>
参数:
		v		: vector / list / set

返回值:
**********************************/
template<class SetSet> void ClearSetSet(SetSet& v)
{
	for(typename SetSet::iterator	it = v.begin();
							it != v.end();
							++it)
	{
		(*it)->clear();
		delete (*it);
	}

	v.clear();
}

template<class SetSet> struct SetSet_Cleaner
{
	static void Clear(SetSet& v) {ClearSetSet(v);}
};

/**********************************
描述: 清除指针集合-集合 (清除前先清除内部指针集合), 适用于 set<vector<Object*>*>
参数:
		v		: vector / list / set

返回值:
**********************************/
template<class PtrSetSet> void ClearPtrSetSet(PtrSetSet& v)
{
	for(typename PtrSetSet::iterator	it = v.begin();
							it != v.end();
							++it)
	{
		ClearPtrSet(**it);
		delete (*it);
	}

	v.clear();
}

template<class PtrSetSet> struct PtrSetSet_Cleaner
{
	static void Clear(PtrSetSet& v) {ClearPtrSetSet(v);}
};

/**********************************
描述: 清除指针数组集合影射 (清除前先清除指针数组集合), 适用于 map<vector<Object*[]>*>
参数:
		v		: vector / list / set

返回值:
**********************************/
template<class PtrArraySetSet> void ClearPtrArraySetSet(PtrArraySetSet& v)
{
	for(typename PtrArraySetSet::iterator	it = v.begin();
									it != v.end();
									++it)
	{
		ClearPtrArraySet(**it);
		delete (*it);
	}

	v.clear();
}

template<class PtrArraySetSet> struct PtrArraySetSet_Cleaner
{
	static void Clear(PtrArraySetSet& v) {ClearPtrArraySetSet(v);}
};

/**********************************
描述: 清除集合影射 (清除前先清除集合), 适用于 map<key, vector<Object>*>
参数:
		v		: map

返回值:
**********************************/
template<class SetMap> void ClearSetMap(SetMap& v)
{
	for(typename SetMap::iterator	it = v.begin();
							it != v.end();
							++it)
	{
		it->second->clear();
		delete it->second;
	}

	v.clear();
}

template<class SetMap> struct SetMap_Cleaner
{
	static void Clear(SetMap& v) {ClearSetMap(v);}
};

/**********************************
描述: 清除指针集合影射 (清除前先清除指针集合), 适用于 map<key, vector<Object*>*>
参数:
		v		: map

返回值:
**********************************/
template<class PtrSetMap> void ClearPtrSetMap(PtrSetMap& v)
{
	for(typename PtrSetMap::iterator	it = v.begin();
							it != v.end();
							++it)
	{
		ClearPtrSet(*(it->second));
		delete it->second;
	}

	v.clear();
}

template<class PtrSetMap> struct PtrSetMap_Cleaner
{
	static void Clear(PtrSetMap& v) {ClearPtrSetMap(v);}
};

/**********************************
描述: 清除指针数组集合影射 (清除前先清除指针数组集合), 适用于 map<key, vector<Object*[]>*>
参数:
		v		: map

返回值:
**********************************/
template<class PtrArraySetMap> void ClearPtrArraySetMap(PtrArraySetMap& v)
{
	for(typename PtrArraySetMap::iterator	it = v.begin();
									it != v.end();
									++it)
	{
		ClearPtrArraySet(*(it->second));
		delete it->second;
	}

	v.clear();
}

template<class PtrArraySetMap> struct PtrArraySetMap_Cleaner
{
	static void Clear(PtrArraySetMap& v) {ClearPtrArraySetMap(v);}
};

/**********************************
描述: 清除映射-影射 (清除前先清除内部映射), 适用于 map<key, map<key2, Object>*>
参数:
v		: map

返回值:
**********************************/
template<class MapMap> void ClearMapMap(MapMap& v)
{
	for(typename MapMap::iterator	it = v.begin();
							it != v.end();
							++it)
	{
		it->second->clear();
		delete it->second;
	}

	v.clear();
}

template<class MapMap> struct MapMap_Cleaner
{
	static void Clear(MapMap& v) {ClearMapMap(v);}
};

/**********************************
描述: 清除指针映射-影射 (清除前先清除指针内部映射), 适用于 map<key, map<key2, Object*>*>
参数:
		v		: map

返回值:
**********************************/
template<class PtrMapMap> void ClearPtrMapMap(PtrMapMap& v)
{
	for(typename PtrMapMap::iterator	it = v.begin();
							it != v.end();
							++it)
	{
		ClearPtrMap(*(it->second));
		delete it->second;
	}

	v.clear();
}

template<class PtrMapMap> struct PtrMapMap_Cleaner
{
	static void Clear(PtrMapMap& v) {ClearPtrMapMap(v);}
};

/**********************************
描述: 清除指针映射-影射 (清除前先清除指针数组内部映射), 适用于 map<key, map<key2, Object*[]>*>
参数:
		v		: map

返回值:
**********************************/
template<class PtrArrayMapMap> void ClearPtrArrayMapMap(PtrArrayMapMap& v)
{
	for(typename PtrArrayMapMap::iterator	it = v.begin();
									it != v.end();
									++it)
	{
		ClearPtrArrayMap(*(it->second));
		delete it->second;
	}

	v.clear();
}

template<class PtrArrayMapMap> struct PtrArrayMapMap_Cleaner
{
	static void Clear(PtrArrayMapMap& v) {ClearPtrArrayMapMap(v);}
};

/************************************************************************/
/*                           指针集合容器                               */
/************************************************************************/
template<class Set, class Cleaner> struct SetWrapper
{
	typedef typename Set::iterator			iterator;
	typedef typename Set::const_iterator	const_iterator;
	typedef typename Set::value_type		value_type;
	typedef typename Set::reference			reference;
	typedef typename Set::const_reference	const_reference;
	typedef typename Set::pointer			pointer;
	typedef typename Set::const_pointer		const_pointer;
	typedef typename Set::size_type			size_type;
	typedef typename Set::difference_type	difference_type;

	SetWrapper()
	{
	}

	virtual ~SetWrapper()
	{
		Clear();
	}

	void Clear()
	{
		if(!IsEmpty())
		{
			Cleaner::Clear(m_set);
		}
	}

	Set& operator *			()			{return m_set;}
	const Set& operator *	()	const	{return m_set;}
	Set* operator ->		()			{return &m_set;}
	const Set* operator ->	()	const	{return &m_set;}
	Set& Get				()			{return m_set;}
	operator Set&			()			{return m_set;}
	bool IsEmpty			()	const	{return m_set.empty();}
	size_t Size				()	const	{return m_set.size();}

protected:
	Set m_set;

	DECLARE_NO_COPY_CLASS(SetWrapper)
};

template<class Set, class Cleaner> struct VectorWrapper : public  SetWrapper<Set, Cleaner>
{
	typedef typename Set::reference			reference_1;
	typedef typename Set::const_reference	const_reference_1;
	typedef typename Set::size_type			size_type_1;
#ifdef __GNUC__
	using SetWrapper<Set, Cleaner>::m_set;
#endif
	VectorWrapper()
	{
	}

	reference_1		operator []	(size_type_1 i)			{return m_set[i];}
	const_reference_1 operator []	(size_type_1 i)	const	{return m_set[i];}

	DECLARE_NO_COPY_CLASS(VectorWrapper)
};

/************************************************************************/
/*                         指针数组集合容器                             */
/************************************************************************/


/************************************************************************/
/*                           指针映射容器                               */
/************************************************************************/
template<class Map, class Cleaner> struct MapWrapper
{
	typedef typename Map::iterator			iterator;
	typedef typename Map::const_iterator	const_iterator;
	typedef typename Map::key_type			key_type;
	typedef typename Map::mapped_type		mapped_type;
	typedef typename Map::value_type		value_type;
	typedef typename Map::reference			reference;
	typedef typename Map::const_reference	const_reference;
	typedef typename Map::pointer			pointer;
	typedef typename Map::size_type			size_type;
	typedef typename Map::difference_type	difference_type;

	MapWrapper()
	{
	}

	~MapWrapper()
	{
		Clear();
	}

	void Clear()
	{
		if(!IsEmpty())
		{
			Cleaner::Clear(m_map);
		}
	}

	Map&				operator *	()								{return m_map;}
	const Map&			operator *	()						const	{return m_map;}
	Map*				operator ->	()								{return &m_map;}
	const Map*			operator ->	()						const	{return &m_map;}
	mapped_type&		operator []	(const key_type& key)			{return m_map[key];}
	const mapped_type&	operator []	(const key_type& key)	const	{return m_map[key];}
	Map& Get			()			{return m_map;}
	operator Map&		()			{return m_map;}
	bool IsEmpty		()	const	{return m_map.empty();}
	size_t Size			()	const	{return m_map.size();}

private:
	Map m_map;

	DECLARE_NO_COPY_CLASS(MapWrapper)
};

/************************************************************************/
/*                            比较仿函数                                */
/************************************************************************/
struct bool_comp_func
{
	bool operator() (bool v1, bool v2) const
	{
		if(!v1)
			return false;
		if(v1 == v2)
			return false;

		return true;
	}
};

template<class T>
// T -> (signed / unsigned) short / int / long / XJ_INT64_T
struct integer_comp_func
{
	bool operator() (T v1, T v2) const
	{
		return v1 < v2;
	}
};

typedef integer_comp_func<short>				short_comp_func;
typedef integer_comp_func<int>					int_comp_func;
typedef integer_comp_func<long>					long_comp_func;
typedef integer_comp_func<XJ_INT64_T>				int64_comp_func;
typedef integer_comp_func<unsigned short>		ushort_comp_func;
typedef integer_comp_func<unsigned int>			uint_comp_func;
typedef integer_comp_func<unsigned long>		ulong_comp_func;
typedef integer_comp_func<XJ_UINT64_T>		uint64_comp_func;

struct float_comp_func
{
	bool operator() (float v1, float v2) const
	{
		float disc	= v1 - v2;
		if(fabsf(disc) < 1E-5)
			return false;

		return disc < 0;
	}
};

struct double_comp_func
{
	bool operator() (double v1, double v2) const
	{
		double disc	= v1 - v2;
		if(fabs(disc) < 1E-8)
			return false;

		return disc < 0;
	}
};

template<class T, bool CASE = false>
// T -> (unsigned) char / wchar_t
struct character_comp_func
{
	bool operator() (T v1, T v2) const
	{
		if(!CASE)
		{
			if(v1 >= 'A' && v1 <= 'Z')	v1 += 32;
			if(v2 >= 'A' && v2 <= 'Z')	v2 += 32;
		}

		return v1 < v2;
	}
};

typedef character_comp_func<char, true>				char_case_comp_func;
typedef character_comp_func<unsigned char, true>	uchar_case_comp_func;
typedef character_comp_func<wchar_t, true>			wchar_case_comp_func;
typedef character_comp_func<char, false>			char_ucase_comp_func;
typedef character_comp_func<unsigned char, false>	uchar_ucase_comp_func;
typedef character_comp_func<wchar_t, false>			wchar_ucase_comp_func;


/************************************************************************/
/*                            排序仿函数                                */
/************************************************************************/
template<bool ASC = true>
struct bool_sort_func
{
	bool operator() (bool v1, bool v2) const
	{
		if(v1 == v2)
			return false;

		bool result = !v1;
		return ASC ? result : !result;
	}
};

typedef bool_sort_func<true>	bool_asc_sort_func;
typedef bool_sort_func<false>	bool_desc_sort_func;

template<class T, bool ASC = true>
// T -> (signed / unsigned) short / int / long / XJ_INT64_T
struct integer_sort_func
{
	bool operator() (T v1, T v2) const
	{
		if(v1 == v2)
			return false;

		bool result = v1 < v2;
		return ASC ? result : !result;
	}
};

typedef integer_sort_func<short,			true>		short_asc_sort_func;
typedef integer_sort_func<unsigned short,	true>		ushort_asc_sort_func;
typedef integer_sort_func<int,				true>		int_asc_sort_func;
typedef integer_sort_func<unsigned int,		true>		uint_asc_sort_func;
typedef integer_sort_func<long,				true>		long_asc_sort_func;
typedef integer_sort_func<unsigned long,	true>		ulong_asc_sort_func;
typedef integer_sort_func<XJ_INT64_T,			true>		int64_asc_sort_func;
typedef integer_sort_func<XJ_UINT64_T,	true>		uint64_asc_sort_func;
typedef integer_sort_func<short,			false>		short_desc_sort_func;
typedef integer_sort_func<unsigned short,	false>		ushort_desc_sort_func;
typedef integer_sort_func<int,				false>		int_desc_sort_func;
typedef integer_sort_func<unsigned int,		false>		uint_desc_sort_func;
typedef integer_sort_func<long,				false>		long_desc_sort_func;
typedef integer_sort_func<unsigned long,	false>		ulong_desc_sort_func;
typedef integer_sort_func<XJ_INT64_T,			false>		int64_desc_sort_func;
typedef integer_sort_func<XJ_UINT64_T,	false>		uint64_desc_sort_func;

template<bool ASC = true>
struct float_sort_func
{
	bool operator() (float v1, float v2) const
	{
		float disc	= v1 - v2;
		if(fabsf(disc) < 1E-5)
			return false;

		bool result = disc < 0;
		return ASC ? result : !result;
	}
};

typedef float_sort_func<true>		float_asc_sort_func;
typedef float_sort_func<false>		float_desc_sort_func;

template<bool ASC = true>
struct double_sort_func
{
	bool operator() (double v1, double v2) const
	{
		double disc	= v1 - v2;
		if(fabs(disc) < 1E-8)
			return false;

		bool result = disc < 0;
		return ASC ? result : !result;
	}
};

typedef double_sort_func<true>		double_asc_sort_func;
typedef double_sort_func<false>		double_desc_sort_func;

template<class T, bool ASC = true, bool CASE = false>
// T -> (unsigned) char / wchar_t
struct character_sort_func
{
	bool operator() (T v1, T v2) const
	{
		if(!CASE)
		{
			if(v1 >= 'A' && v1 <= 'Z')	v1 += 32;
			if(v2 >= 'A' && v2 <= 'Z')	v2 += 32;
		}

		if(v1 == v2)
			return false;

		bool result = v1 < v2;
		return ASC ? result : !result;
	}
};

typedef character_sort_func<char, true, true>				char_asc_case_sort_func;
typedef character_sort_func<unsigned char, true, true>		uchar_asc_case_sort_func;
typedef character_sort_func<wchar_t, true, true>			wchar_asc_case_sort_func;
typedef character_sort_func<char, true, false>				char_asc_ucase_sort_func;
typedef character_sort_func<unsigned char, true, false>		uchar_asc_ucase_sort_func;
typedef character_sort_func<wchar_t, true, false>			wchar_asc_ucase_sort_func;
typedef character_sort_func<char, false, true>				char_desc_case_sort_func;
typedef character_sort_func<unsigned char, false, true>		uchar_desc_case_sort_func;
typedef character_sort_func<wchar_t, false, true>			wchar_desc_case_sort_func;
typedef character_sort_func<char, false, false>				char_desc_ucase_sort_func;
typedef character_sort_func<unsigned char, false, false>	uchar_desc_ucase_sort_func;
typedef character_sort_func<wchar_t, false, false>			wchar_desc_ucase_sort_func;

/************************************************************************/
/*					   smart_ptr 单实体或数组智能指针                    */
/************************************************************************/

template<class _Ty>
struct simple_deleter
{
	static void delete_ptr(_Ty* pv) {delete pv;}
};

template<class _Ty>
struct global_simple_deleter
{
	static void delete_ptr(_Ty* pv) {::delete pv;}
};

template<class _Ty>
struct array_deleter
{
	static void delete_ptr(_Ty* pv) {delete[] pv;}
};

template<class _Ty>
struct global_array_deleter
{
	static void delete_ptr(_Ty* pv) {::delete[] pv;}
};

template<class _Ty, class _Deleter>
class smart_ptr
{
public:
	smart_ptr(_Ty* _Ptr = 0)					: _Myptr(_Ptr)				{}
	smart_ptr(smart_ptr<_Ty, _Deleter>& _Right)	: _Myptr(_Right.release())	{}

	~smart_ptr()
	{
		reset();
	}

	smart_ptr<_Ty, _Deleter>& reset(_Ty* _Ptr = 0)
	{
		if (_Ptr != _Myptr)
		{
			if(_Myptr)
				_Deleter::delete_ptr(_Myptr);

			_Myptr = _Ptr;
		}

		return *this;
	}

	smart_ptr<_Ty, _Deleter>& reset(smart_ptr<_Ty, _Deleter>& _Right)
	{
		if (this != &_Right)
			reset(_Right.release());

		return *this;
	}

	_Ty* release()
	{
		_Ty* _Ptr	= _Myptr;
		_Myptr		= 0;

		return _Ptr;
	}

	smart_ptr<_Ty, _Deleter>& operator = (_Ty* _Ptr)						{return reset(_Ptr);}
	smart_ptr<_Ty, _Deleter>& operator = (smart_ptr<_Ty, _Deleter>& _Right)	{return reset(_Right);}

	bool is_valid		()	const	{return _Myptr != 0;}
	_Ty& operator *		()	const	{return *_Myptr;}
	_Ty* get			()	const	{return _Myptr;}
	_Ty* operator ->	()	const	{return _Myptr;}
	operator _Ty*		()	const	{return _Myptr;}

private:
	template<class _Other> smart_ptr<_Ty, _Deleter>					(const smart_ptr<_Ty, _Other>&);
	template<class _Other> smart_ptr<_Ty, _Deleter>&	reset		(const smart_ptr<_Ty, _Other>&);
	template<class _Other> smart_ptr<_Ty, _Deleter>&	operator =	(const smart_ptr<_Ty, _Other>&);

	template<class _Other> smart_ptr<_Ty, _Deleter>					(const smart_ptr<_Other, _Deleter>&);
	template<class _Other> smart_ptr<_Ty, _Deleter>&	reset		(const smart_ptr<_Other, _Deleter>&);
	template<class _Other> smart_ptr<_Ty, _Deleter>&	operator =	(const smart_ptr<_Other, _Deleter>&);

protected:
	_Ty* _Myptr;
};


/************************************************************************/
/*				    smart_simple_ptr 单实体智能指针                      */
/************************************************************************/

template<class _Ty>
class smart_simple_ptr : public smart_ptr<_Ty, simple_deleter<_Ty> >
{
public:
	smart_simple_ptr(_Ty* _Ptr = 0)									: smart_ptr<_Ty, simple_deleter<_Ty> >::smart_ptr(_Ptr)	{}
	smart_simple_ptr(smart_simple_ptr<_Ty>& _Right)					: smart_ptr<_Ty, simple_deleter<_Ty> >::smart_ptr(_Right)	{}
	smart_simple_ptr(smart_ptr<_Ty, simple_deleter<_Ty> >& _Right)	: smart_ptr<_Ty, simple_deleter<_Ty> >::smart_ptr(_Right)	{}

	smart_simple_ptr<_Ty>& operator = (smart_ptr<_Ty, simple_deleter<_Ty> >& _Right)
	{return (smart_simple_ptr<_Ty>&)smart_ptr<_Ty, simple_deleter<_Ty> >::operator = (_Right);}

	smart_simple_ptr<_Ty>& operator = (smart_simple_ptr<_Ty>& _Right)
	{return (smart_simple_ptr<_Ty>&)smart_ptr<_Ty, simple_deleter<_Ty> >::operator = (_Right);}

	smart_simple_ptr<_Ty>& operator = (_Ty* _Ptr)
	{return (smart_simple_ptr<_Ty>&)smart_ptr<_Ty, simple_deleter<_Ty> >::operator = (_Ptr);}

private:
	template<class _Other> smart_simple_ptr<_Ty>				(const smart_ptr<_Ty, _Other>&);
	template<class _Other> smart_simple_ptr<_Ty>&	operator =	(const smart_ptr<_Ty, _Other>&);

	template<class _Other> smart_simple_ptr<_Ty>				(const smart_simple_ptr<_Other>&);
	template<class _Other> smart_simple_ptr<_Ty>&	operator =	(const smart_simple_ptr<_Other>&);
};

/************************************************************************/
/*		   smart_gd_simple_ptr 单实体智能指针 (使用全局 delete)          */
/************************************************************************/

template<class _Ty>
class smart_gd_simple_ptr : public smart_ptr<_Ty, global_simple_deleter<_Ty> >
{
public:
	smart_gd_simple_ptr(_Ty* _Ptr = 0)										: smart_ptr<_Ty, global_simple_deleter<_Ty> >::smart_ptr(_Ptr)	{}
	smart_gd_simple_ptr(smart_gd_simple_ptr<_Ty>& _Right)					: smart_ptr<_Ty, global_simple_deleter<_Ty> >::smart_ptr(_Right)	{}
	smart_gd_simple_ptr(smart_ptr<_Ty, global_simple_deleter<_Ty> >& _Right)	: smart_ptr<_Ty, global_simple_deleter<_Ty> >::smart_ptr(_Right)	{}

	smart_gd_simple_ptr<_Ty>& operator = (smart_ptr<_Ty, global_simple_deleter<_Ty> >& _Right)
	{return (smart_gd_simple_ptr<_Ty>&)smart_ptr<_Ty, global_simple_deleter<_Ty> >::operator = (_Right);}

	smart_gd_simple_ptr<_Ty>& operator = (smart_gd_simple_ptr<_Ty>& _Right)
	{return (smart_gd_simple_ptr<_Ty>&)smart_ptr<_Ty, global_simple_deleter<_Ty> >::operator = (_Right);}

	smart_gd_simple_ptr<_Ty>& operator = (_Ty* _Ptr)
	{return (smart_gd_simple_ptr<_Ty>&)smart_ptr<_Ty, global_simple_deleter<_Ty> >::operator = (_Ptr);}

private:
	template<class _Other> smart_gd_simple_ptr<_Ty>					(const smart_ptr<_Ty, _Other>&);
	template<class _Other> smart_gd_simple_ptr<_Ty>&	operator =	(const smart_ptr<_Ty, _Other>&);

	template<class _Other> smart_gd_simple_ptr<_Ty>					(const smart_gd_simple_ptr<_Other>&);
	template<class _Other> smart_gd_simple_ptr<_Ty>&	operator =	(const smart_gd_simple_ptr<_Other>&);
};

/************************************************************************/
/*                   smart_array_ptr 数组智能指针                        */
/************************************************************************/

template<class _Ty>
class smart_array_ptr : public smart_ptr<_Ty, array_deleter<_Ty> >
{
public:
	smart_array_ptr(_Ty* _Ptr = 0)								: smart_ptr<_Ty, array_deleter<_Ty> >::smart_ptr(_Ptr)	{}
	smart_array_ptr(smart_simple_ptr<_Ty>& _Right)				: smart_ptr<_Ty, array_deleter<_Ty> >::smart_ptr(_Right)	{}
	smart_array_ptr(smart_ptr<_Ty, array_deleter<_Ty> >& _Right)	: smart_ptr<_Ty, array_deleter<_Ty> >::smart_ptr(_Right)	{}

	smart_array_ptr<_Ty>& operator = (smart_ptr<_Ty, array_deleter<_Ty> >& _Right)
	{return (smart_array_ptr<_Ty>&)smart_ptr<_Ty, array_deleter<_Ty> >::operator = (_Right);}

	smart_array_ptr<_Ty>& operator = (smart_array_ptr<_Ty>& _Right)
	{return (smart_array_ptr<_Ty>&)smart_ptr<_Ty, array_deleter<_Ty> >::operator = (_Right);}

	smart_array_ptr<_Ty>& operator = (_Ty* _Ptr)
	{return (smart_array_ptr<_Ty>&)smart_ptr<_Ty, array_deleter<_Ty> >::operator = (_Ptr);}

private:
	template<class _Other> smart_array_ptr<_Ty>					(const smart_ptr<_Ty, _Other>&);
	template<class _Other> smart_array_ptr<_Ty>&	operator =	(const smart_ptr<_Ty, _Other>&);

	template<class _Other> smart_array_ptr<_Ty>					(const smart_array_ptr<_Other>&);
	template<class _Other> smart_array_ptr<_Ty>&	operator =	(const smart_array_ptr<_Other>&);
};

/************************************************************************/
/*          smart_gd_array_ptr 单实体智能指针 (使用全局 delete)          */
/************************************************************************/

template<class _Ty>
class smart_gd_array_ptr : public smart_ptr<_Ty, global_array_deleter<_Ty> >
{
public:
	smart_gd_array_ptr(_Ty* _Ptr = 0)										: smart_ptr<_Ty, global_array_deleter<_Ty> >::smart_ptr(_Ptr)	{}
	smart_gd_array_ptr(smart_gd_array_ptr<_Ty>& _Right)						: smart_ptr<_Ty, global_array_deleter<_Ty> >::smart_ptr(_Right)	{}
	smart_gd_array_ptr(smart_ptr<_Ty, global_array_deleter<_Ty> >& _Right)	: smart_ptr<_Ty, global_array_deleter<_Ty> >::smart_ptr(_Right)	{}

	smart_gd_array_ptr<_Ty>& operator = (smart_ptr<_Ty, global_array_deleter<_Ty> >& _Right)
	{return (smart_gd_array_ptr<_Ty>&)smart_ptr<_Ty, global_array_deleter<_Ty> >::operator = (_Right);}

	smart_gd_array_ptr<_Ty>& operator = (smart_gd_array_ptr<_Ty>& _Right)
	{return (smart_gd_array_ptr<_Ty>&)smart_ptr<_Ty, global_array_deleter<_Ty> >::operator = (_Right);}

	smart_gd_array_ptr<_Ty>& operator = (_Ty* _Ptr)
	{return (smart_gd_array_ptr<_Ty>&)smart_ptr<_Ty, global_array_deleter<_Ty> >::operator = (_Ptr);}

private:
	template<class _Other> smart_gd_array_ptr<_Ty>				(const smart_ptr<_Ty, _Other>&);
	template<class _Other> smart_gd_array_ptr<_Ty>&	operator =	(const smart_ptr<_Ty, _Other>&);

	template<class _Other> smart_gd_array_ptr<_Ty>				(const smart_gd_array_ptr<_Other>&);
	template<class _Other> smart_gd_array_ptr<_Ty>&	operator =	(const smart_gd_array_ptr<_Other>&);
};



} /* namespace XJUtilClass */

#endif /* SRC_XJSTLHELPER_H_ */
